﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ece750project
{
    struct List1_type
    {
        public int ID;
        public bool Granted;
    }
    public partial class PermitDoctors : Form
    {
        public PermitDoctors()
        {
            InitializeComponent();
        }
        private void Save_permissions()
        {
            for (int i = 0; i <= doctor_list.Items.Count - 1; i++)
            {
                List1_type Tag;
                ListViewItem IT = doctor_list.Items[i];
                Tag = (List1_type)IT.Tag;
                if (IT.Checked != Tag.Granted)
                {
                    if (IT.Checked == true)
                    {
                        DB_BusinessRules.Grant_doc_permission(Tag.ID, abc_app.permitDoctors_para.Patient_id, abc_app.permitDoctors_para.Visit_id, abc_app.current_user_personal_info.person_id);
                    }
                    else
                    {
                        DB_BusinessRules.Revok_doc_permission(Tag.ID, abc_app.permitDoctors_para.Patient_id, abc_app.permitDoctors_para.Visit_id, abc_app.current_user_personal_info.person_id);
                    }
                }
            }
        }
        private void Fill_permission_list()
        {
            DataTable DT = DB_BusinessRules.Get_docs_list();
            doctor_list.Items.Clear();
            for (int i = 0; i <= DT.Rows.Count - 1; i++)
            {
                int doc_id;
                string doc_name;
                string dep_name;
                List1_type Tag;
                doc_id = (int)DT.Rows[i]["person_id"];
                doc_name = (string)DT.Rows[i]["first_name"] + (string)DT.Rows[i]["last_name"];
                dep_name = (string)DT.Rows[i]["dept_name"];
                Tag.ID = doc_id;

                if (doc_id == abc_app.current_user_personal_info.person_id) continue;
                ListViewItem IT = doctor_list.Items.Add("D" + doc_id.ToString(), "", "");
                IT.SubItems.Add(doc_name);
                IT.SubItems.Add(dep_name);
                
                bool  res=DB_BusinessRules.Is_Doc_permited_to_vist(doc_id, abc_app.permitDoctors_para.Patient_id, abc_app.permitDoctors_para.Visit_id);
                if (res == true) IT.Checked = true; else IT.Checked = false;
                Tag.Granted = IT.Checked;
                IT.Tag = Tag;
            }

        }
        private void PermitDoctors_Load(object sender, EventArgs e)
        {
            Fill_permission_list();
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            Save_permissions();
            this.Close();
        }

        private void doctor_list_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
